<template>
  <div class="success-case-list-container">
    <h2 class="page-title">成功案例管理</h2>
    
    <el-card shadow="never" class="toolbar-card">
      <el-button type="primary" icon="Plus" @click="$router.push('/success-case/create')">新增案例</el-button>
      <el-button icon="Refresh" @click="loadList">刷新</el-button>
    </el-card>
    
    <el-card shadow="never" class="table-card">
      <el-table v-loading="loading" :data="list" stripe>
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="caseNo" label="案例编号" width="120" />
        <el-table-column prop="maleUserNickname" label="男方昵称" width="120" />
        <el-table-column prop="femaleUserNickname" label="女方昵称" width="120" />
        <el-table-column prop="quote" label="案例引言" min-width="200" show-overflow-tooltip />
        <el-table-column prop="marriageDate" label="结婚日期" width="120" />
        <el-table-column prop="matchmakerName" label="红娘" width="100" />
        <el-table-column prop="caseStatus" label="状态" width="80">
          <template #default="{ row }">
            <el-switch v-model="row.caseStatus" :active-value="1" :inactive-value="0" @change="handleStatusChange(row)" />
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="创建时间" width="180" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" link @click="$router.push(`/success-case/edit/${row.caseId}`)">编辑</el-button>
            <el-button type="danger" size="small" link @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next"
          @size-change="loadList"
          @current-change="loadList"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/utils/request'
import { API_ENDPOINTS } from '@/config/api'

const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const list = ref([])

const loadList = async () => {
  loading.value = true
  try {
    const response = await request.get(API_ENDPOINTS.CASE_LIST, {
      params: { page: currentPage.value, pageSize: pageSize.value }
    })
    if (response.code === 200) {
      list.value = response.data.list || response.data || []
      total.value = response.data.total || list.value.length
    }
  } catch (error) {
    console.error('加载失败:', error)
    ElMessage.error('加载失败')
  } finally {
    loading.value = false
  }
}

const handleStatusChange = async (row) => {
  try {
    const response = await request.put(`${API_ENDPOINTS.CASE_UPDATE}/${row.caseId}`, { caseStatus: row.caseStatus })
    if (response.code === 200) {
      ElMessage.success('状态更新成功')
    } else {
      row.caseStatus = row.caseStatus === 1 ? 0 : 1
    }
  } catch (error) {
    console.error('状态更新失败:', error)
    row.caseStatus = row.caseStatus === 1 ? 0 : 1
  }
}

const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除这个案例吗？', '提示', { type: 'warning' })
    const response = await request.delete(`${API_ENDPOINTS.CASE_DELETE}/${row.caseId}`)
    if (response.code === 200) {
      ElMessage.success('删除成功')
      loadList()
    }
  } catch (error) {
    if (error !== 'cancel') console.error('删除失败:', error)
  }
}

onMounted(() => loadList())
</script>

<style scoped>
.success-case-list-container { padding: 0; }
.page-title { font-size: 24px; font-weight: bold; color: #333; margin: 0 0 20px 0; }
.toolbar-card { margin-bottom: 20px; }
.table-card { margin-top: 20px; }
.pagination-container { display: flex; justify-content: flex-end; margin-top: 20px; }
</style>

